//
//  GroupUserViewController.m
//  SportSocial
//
//  Created by yull on 15/1/29.
//  Copyright (c) 2015年 yull. All rights reserved.
//

#import "GroupUserViewController.h"
#import "NearUser.h"
#import "GroupTitleCell.h"
#import "FriendCell.h"
#import "FriendViewController.h"
#import "GroupNet.h"

@interface GroupUserViewController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *userTableView;

@end

@implementation GroupUserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavibar];
    
    CGRect rect = CGRectMake(0, 0, self.view.width, self.view.height);
    self.userTableView = [[UITableView alloc] initWithFrame:rect];
    self.userTableView.autoresizingMask = self.view.autoresizingMask;
    self.userTableView.dataSource = self;
    self.userTableView.delegate = self;
    [self.view addSubview:self.userTableView];
    
    self.userTableView.tableFooterView = [[UIView alloc] init];
}

- (void)initNavibar
{
    [self addCommonBackButton];
    self.navigationItem.title = @"群成员";
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.editEnable ? 2 : 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0 && self.editEnable) {
        return 1;
    }else {
        return self.userArray.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && self.editEnable) {
        return 40.0f;
    }else {
        return [FriendCell friendCellDefaultHeight];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && self.editEnable) {
        GroupTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddUserCell"];
        if (!cell) {
            cell = [[GroupTitleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AddUserCell"];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.textLabel.text = @"添加新成员";
        }
        return cell;
    }else {
        FriendCell *userCell = [tableView dequeueReusableCellWithIdentifier:@"UserCell"];
        if (!userCell) {
            userCell = [[FriendCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UserCell"];
        }
        [userCell drawCellWithFriendInfo:[self.userArray objectAtIndex:indexPath.row]];
        return userCell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && self.editEnable) {
        FriendViewController *controller = [[FriendViewController alloc] init];
        controller.fromAddAction = YES;
        controller.groupId = self.groudId;
        [self.navigationController pushViewController:controller animated:YES];
    } else {
        
        UserModel *user = [self.userArray objectAtIndex:indexPath.row];
        
        UIViewController *controller = nil;
        if (user.userId == [CurrentUser shareInstance].userId) {
            controller = [Interface createMyInfoViewController];
        } else {
            controller = [Interface createUserInfoViewControllerWithUserId:user.userId userName:user.name];
        }
        
        [self.navigationController pushViewController:controller animated:YES];
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.editEnable) {
        return UITableViewCellEditingStyleDelete;
    }
    
    return UITableViewCellEditingStyleNone;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    UserModel *user = [self.userArray objectAtIndex:indexPath.row];
    
    if (user.userId == [CurrentUser shareInstance].userId) {
        return NO;
    }
    
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    UserModel *user = [self.userArray objectAtIndex:indexPath.row];
    
    [self showHUD];
    
    [GroupNet kickUser:user.userId fromGroup:self.groudId successBlock:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self hideHUD];
        
        BOOL success = [[responseObject objectForKey:@"success"] boolValue];
        if (success) {
            
            [self.userArray removeObjectAtIndex:indexPath.row];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"DeleteGroupMember" object:nil userInfo:@{@"user":user}];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self showText:@"删除成功！" time:2];
                [self.userTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            });
        } else {
            NSString *message = [responseObject objectForKey:@"message"];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self showText:message time:2];
            });
        }
    } failBlock:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self hideHUD];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self showText:@"网络连接失败" time:2];
        });
    }];
}

- (void)dealloc
{
    self.userTableView.delegate = nil;
    self.userTableView.dataSource = nil;
}
@end
